package com.zyf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyf.mapper.PrescriptionMapper;
import com.zyf.pojo.DrugInfo;
import com.zyf.pojo.Prescription;
import com.zyf.service.DrugInfoService;
import com.zyf.mapper.DrugInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author zhenyf
* @description 针对表【drug_info】的数据库操作Service实现
* @createDate 2024-10-24 19:07:01
*/
@Service
public class DrugInfoServiceImpl extends ServiceImpl<DrugInfoMapper, DrugInfo>
    implements DrugInfoService{


    @Autowired
    private DrugInfoMapper drugInfoMapper;
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Override
    public List<DrugInfo> queryDrugRegisterListService(Integer registerId) {
        return drugInfoMapper.queryDrugRegisterMapper(registerId);
    }

    @Transactional
    @Override
    public void updatePreDrugService(Integer pid
            , Integer did, Integer drugNumber) {
        //查询某个药品信息
        DrugInfo drugInfo = drugInfoMapper.selectById(did);
        drugInfo.setDrugStore(drugInfo.getDrugStore()-drugNumber);
        //修改药品库存
        drugInfoMapper.updateById(drugInfo);

        Prescription prescription=new Prescription();
        prescription.setId(pid);
        prescription.setDrugState("已发药");
        //修改处方状态
        prescriptionMapper.updateById(prescription);

    }

    @Override
    public Map<String, Object> queryDurgPageService(Integer pageNum
            , Integer pageSize, DrugInfo drugInfo) {
        Map<String, Object> result=new HashMap<>();

        //指定分页查询参数
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        //查询数据库
        List<DrugInfo> drugInfos = drugInfoMapper.queryDrugListPageMapper(drugInfo);

        result.put("total",page.getTotal());
        result.put("drugInfos",drugInfos);
        return result;
    }
}




